# 提取CSDN学院首页中的课程分类
import lxml.etree as le
import urllib.request as ur


request = ur.Request(
    url='https://edu.csdn.net/',
    headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'}
)
response = ur.urlopen(request).read()
data_x_s = le.HTML(response).xpath('//div[contains(@class,"classify_cList")]')  # 这个结果仍然是xpath对象
data_s = []
for data_x in data_x_s:
    # 对上个Xpath的结果在行进Xpath数据提取，需要加 .
    category1 = data_x.xpath('./h3/a/text()')[0]
    category2_s = data_x.xpath('./div/span/a/text()')  # 这个结果转成了text，列表包含着字符串
    data_s.append(
        dict(category1=category1, category2=category2_s)
    )

for data in data_s:
    print(data['category1'])
    for category2 in data['category2']:
        print("     ",category2)


